package com.emcom.gotour.dao.ibatis;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.web.util.HtmlUtils;

import com.emcom.gotour.dao.TourSchedulerDao;
import com.emcom.gotour.po.TourScheduler;
import com.emcom.gotour.utils.WebUtils;
import com.ibatis.sqlmap.client.SqlMapClient;

public class IbatisTourSchedulerDao implements TourSchedulerDao {

	private SqlMapClientTemplate sqlMapClientTemplate;

	public TourScheduler query(long id) {
		TourScheduler tourScheduler = new TourScheduler();
		tourScheduler.setId(id);
		List<TourScheduler> tourSchedulerList = query(tourScheduler);
		if(tourSchedulerList.size() == 1) {
			return tourSchedulerList.get(0);
		}
		return null;
	}

	public List<TourScheduler> query(TourScheduler tourScheduler) {
		List<TourScheduler> tourSchedulerList = sqlMapClientTemplate.queryForList("TourScheduler.query", tourScheduler);
		List<TourScheduler> resultList = new ArrayList<TourScheduler>();
		for(TourScheduler tourSchedulerObject : tourSchedulerList) {
			resultList.add(htmlEscape(tourSchedulerObject));
		}
		return resultList;
	}

	public void update(TourScheduler tourScheduler) {
		sqlMapClientTemplate.update("TourScheduler.update", tourScheduler);
	}

	public void insert(TourScheduler tourScheduler) {
		sqlMapClientTemplate.insert("TourScheduler.insert", tourScheduler);
	}

	public void delete(TourScheduler tourScheduler) {
		sqlMapClientTemplate.delete("TourScheduler.delete", tourScheduler);
	}

	public void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClientTemplate = new SqlMapClientTemplate();
		this.sqlMapClientTemplate.setSqlMapClient(sqlMapClient);
	}

	private TourScheduler htmlEscape(TourScheduler tourScheduler) {
		// -- html
		tourScheduler.setContent(HtmlUtils.htmlEscape(tourScheduler.getContent()));
		tourScheduler.setComments(HtmlUtils.htmlEscape(tourScheduler.getComments()));
		tourScheduler.setDinner(HtmlUtils.htmlEscape(tourScheduler.getDinner()));
		tourScheduler.setHotel(HtmlUtils.htmlEscape(tourScheduler.getHotel()));
		tourScheduler.setName(HtmlUtils.htmlEscape(tourScheduler.getName()));
		// -- \n
		tourScheduler.setContent(WebUtils.htmlEscape(tourScheduler.getContent()));
		tourScheduler.setComments(WebUtils.htmlEscape(tourScheduler.getComments()));
		tourScheduler.setDinner(WebUtils.htmlEscape(tourScheduler.getDinner()));
		tourScheduler.setHotel(WebUtils.htmlEscape(tourScheduler.getHotel()));
		tourScheduler.setName(WebUtils.htmlEscape(tourScheduler.getName()));
		return tourScheduler;
	}

}
